﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System.Security.Claims;

namespace Basic.Identity4;

public class LocalAuthenticationService : AuthenticationService
{
    public LocalAuthenticationService(
        IAuthenticationSchemeProvider schemes,
        IAuthenticationHandlerProvider handlers,
        IClaimsTransformation transform,
        IOptions<AuthenticationOptions> options) : base(schemes, handlers, transform, options)
    {
    }

    public override async Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string? scheme)
    {
        IHeaderDictionary header = context.Request.Headers;
        /* Token字符串解析出来的JwtSecurityToken */
        if (header.TryGetValue("Authorization", out var tokenStr) && AuthorizationUtils.VerifyToken(tokenStr, out ClaimsPrincipal? cp))
            return AuthenticateResult.Success(new AuthenticationTicket(cp, scheme));
        return AuthenticateResult.Fail("");
    }

    //public override async Task ChallengeAsync(HttpContext context, string? scheme, AuthenticationProperties? properties)
    //{
    //    await base.ChallengeAsync(context, scheme, properties);
    //}

    //public override async Task ForbidAsync(HttpContext context, string? scheme, AuthenticationProperties? properties)
    //{
    //    await base.ForbidAsync(context, scheme, properties);
    //}

    //public override async Task SignInAsync(HttpContext context, string? scheme, ClaimsPrincipal principal, AuthenticationProperties? properties)
    //{
    //    await base.SignInAsync(context, scheme, principal, properties);
    //}

    //public override async Task SignOutAsync(HttpContext context, string? scheme, AuthenticationProperties? properties)
    //{
    //    await base.SignOutAsync(context, scheme, properties);
    //}

}